package com.dbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dbms.domain.dto.BisXajmodeParasDTO;
import com.dbms.domain.entity.BisXajmodeParas;
import com.dbms.domain.vo.BisXajmodeParasVO;
import com.dbms.mapper.BisXajmodeParasMapper;
import com.dbms.service.BisXajmodeParasService;
import com.dbms.utils.SnowflakeIdGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class BisXajmodeParasServiceImpl implements BisXajmodeParasService {

    final BisXajmodeParasMapper mapper;
    final SnowflakeIdGenerator snowflakeIdGenerator;

    @Override
    public BisXajmodeParasVO getInfoByStationCode(String stationCode) {
        BisXajmodeParas paras = mapper.queryInfo(stationCode);
        if(ObjectUtils.isNotEmpty(paras)) {
            BisXajmodeParasVO vo = new BisXajmodeParasVO();
            BeanUtils.copyProperties(paras, vo);
            return vo;
        }else{
            return null;
        }
    }

    @Override
    public BisXajmodeParas getInfoByStationCode2(String stationCode) {
        return mapper.queryInfo(stationCode);
    }

    @Override
    public boolean saveOrUpdate(BisXajmodeParasDTO dto) {
        if(StringUtils.isBlank(dto.getId())) {
            if(mapper.selectCount(new LambdaQueryWrapper<BisXajmodeParas>().eq(BisXajmodeParas::getStcd,dto.getStcd()))>0 ){
                throw new RuntimeException("该数据已存在");
            }
            BisXajmodeParas paras = new BisXajmodeParas();
            BeanUtils.copyProperties(dto,paras);
            paras.setId(snowflakeIdGenerator.nextId().toString());
            mapper.insert(paras);
        }else{
            BisXajmodeParas paras = mapper.queryInfoById(dto.getId());
            BeanUtils.copyProperties(dto,paras);
            mapper.updateByPrimaryKeySelective(paras);
        }

        return Boolean.TRUE;
    }

    @Override
    public boolean update(BisXajmodeParas paras) {
        BisXajmodeParas bisXajmodeParas = mapper.selectById(paras.getId());
        BeanUtils.copyProperties(paras,bisXajmodeParas);
        mapper.updateByPrimaryKeySelective(bisXajmodeParas);
        return Boolean.TRUE;
    }
}
